package com.ybkj.daijia.server.event.listener;

import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.DriverRechargeEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.reactor.ReactorVo;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.math.BigDecimal;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class DriverRechargeEventListener implements ApplicationListener<DriverRechargeEvent> {

    private static final Logger logger = LoggerFactory.getLogger(DriverRechargeEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;
    @Autowired
    private SmsService smsService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Override
    @Async
    public void onApplicationEvent(DriverRechargeEvent event) {
        logger.debug("driver Recharge event:{}", "");

        ReactorVo vo = (ReactorVo) event.getSource();
        Driver driver = vo.getDriver();
        BigDecimal money = vo.getMoney();
        String type = vo.getType();

        Company maincompany = companyService.getMain();

        if (StringUtils.isNotBlank(maincompany.getMobilePhone())) {
            SmsTemplet rechargeTemplet = smsService
                .findByAlias(driver.getCompanyId(), SmsType.driverRecharge);
            Company driverCompany = companyService.findOne(driver.getCompanyId());
            Company finalCompany = driverCompany;
            Company main = companyService.getMain();
            Setting setting = settingUtils.get();
            if (!main.getId().equals(driverCompany.getId()) && setting.isSmsMainCompany()) {
                finalCompany = main;
            }
            if (null != rechargeTemplet && StringUtils.isNotBlank(rechargeTemplet.getContent())) {
                try {

                    String typeStr = "";
                    if ("admin".equals(type)) {
                        typeStr = "后台充值";
                    } else if ("alipay".equals(type)) {
                        typeStr = "支付宝充值";
                    } else if ("weixin".equals(type)) {
                        typeStr = "微信充值";
                    } else if ("unionpay".equals(type)) {
                        typeStr = "银联充值";
                    }
                    //smsService.sendSms(maincompany.getMobilePhone(), "recharge", "{\"名称\":\""+"服务人员【"+driver.getRealname()+"】"+"\",\"充值金额\":\""+ money+"元"+"\",\"充值方式\":\""+typeStr+"\",\"电话\":\""+driver.getPhone()+"\"}", finalCompany.getId(), 0, finalCompany.getSignName());
                    //发送短信（改）
                    JSONObject smsParam = new JSONObject();
                    smsParam.put("name", driver.getRealname());
                    smsParam.put("typeStr", typeStr);
                    smsParam.put("tel", driver.getPhone());
                    smsService
                        .sendSms(maincompany.getMobilePhone(), rechargeTemplet.getTemplateId(),
                            smsParam.toJSONString(), finalCompany.getId(), 0,
                            finalCompany.getSignName());
                } catch (Exception e) {
                    logger.error("发送短信发生异常：{}", e.getMessage());
                }
            }
        }


    }

}
